package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.ArticleManagement;
import com.ruoyi.system.domain.PageArtcle;
import com.ruoyi.system.domain.ShopClassProductDetail;
import com.ruoyi.system.service.ShopClassProductDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/***
 * 说明
 * @author:ZZ
 * @date: 2024-12-10  23:26
 **/

@CrossOrigin
@RestController
@RequestMapping("/system/product")
@Api(tags = "商品相关信息接口")
public class ShopClassProductDetailController  extends BaseController {

    @Autowired
    private ShopClassProductDetailService service;




    @GetMapping("/getPriceOrBrandDetailList")
    @ApiOperation("  商品展示页面 左侧  价格区间接口  和 品牌 调用接口，" +
            "  返回结果集合  【 符合对应价格区间以及对应品牌的商品信息集合 】")
    public Object getPriceOrBrandDetailList(@RequestParam(value = "minPrice",required = false) String minPrice,
                                            @RequestParam(value = "maxPrice",required = false) String maxPrice,
                                            @RequestParam(value = "brand",required = false) String brand) {
        // minPrice,maxPrice,brand
        QueryWrapper<ShopClassProductDetail> wrapper = new QueryWrapper<>();
        wrapper.like((null !=brand && !brand.isEmpty()),"title",brand);
        wrapper.between("price",minPrice,maxPrice);
        Object result = service.list(wrapper);
        return success(result);
    }



    @GetMapping("/list")
    @ApiOperation("商品信息列表   带分页")
//    public Object list( ShopClassProductDetail pageArtcle){
    public Object list(@RequestParam(value = "pageNum",required = false)Integer pageNum,
                       @RequestParam(value = "pageSize",required = false)Integer pageSize,
                       @RequestParam(value = "tradeName",required = false)String tradeName,
                       @RequestParam(value = "className",required = false)String className
                       ){
        if (pageNum == null || pageSize ==null){
            return error("参数不能为空 ！");
        }
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<ShopClassProductDetail> wrapper = new QueryWrapper<>();
        wrapper.like(tradeName!=null && !tradeName.isEmpty(),"title",tradeName);
        wrapper.like(className!=null && !className.isEmpty(),"class_name",className);
        wrapper.eq("frame",0);
        List<ShopClassProductDetail> list = service.list(wrapper);
        PageInfo<ShopClassProductDetail> of = PageInfo.of(list);
        return  success(of);
    }


    @GetMapping("/listByClass")
    @ApiOperation("获取指定分类的所关联商品信息列表   带分页")
    public Object listByClass(  @RequestParam(value = "pageNum",required = false)Integer pageNum,
                                @RequestParam(value = "pageSize",required = false)Integer pageSize,
                                @RequestParam(value = "fileName",required = false)String className){
        if (pageNum == null || pageSize == null){
            return error("参数不能为空 ！");
        }
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<ShopClassProductDetail> wrapper = new QueryWrapper<>();
        wrapper.like(className!=null && !className.isEmpty(),"class_name",className);
//        wrapper.eq("frame",0);
        List<ShopClassProductDetail> list = service.list(wrapper);
        PageInfo<ShopClassProductDetail> of = PageInfo.of(list);
        return  success(of);
    }

    @PostMapping("/save")
    @ApiOperation("保存商品信息")
    public Object saveArticle( ShopClassProductDetail pageArtcle) throws IOException {
        if (pageArtcle == null){
            return error("参数不能为空 ！");
        }
        Object o = service.save(pageArtcle);
        return o;
    }


    /**
     * 上架 下架 状态修改
     *  是否上下架（1--上架2--下架）
     * */
    @PutMapping("shelvesModified")
    @ApiOperation("商品上下架")
    public AjaxResult shelvesModified(ShopClassProductDetail pageArtcle){
        if(null == pageArtcle){
            return success("参数不能为空");
        }
        UpdateWrapper<ShopClassProductDetail> wrapper = new UpdateWrapper<>();
        wrapper.set("frame",pageArtcle.getFrame());
        wrapper.eq("id",pageArtcle.getId());
        boolean flag = service.update(wrapper);
        return flag ?success("操作成功"):success("操作失败");
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @ApiOperation("删除指定数据")
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable("ids") List<Long> ids) {
        if (ids == null || ids.size()==0){
            return success("参数不能为空");
        }
        service.removeBatchByIds(ids);
        return  success("删除成功");
    }


    //获取单条数据详情
    @GetMapping(value = "{id}")
    public AjaxResult selectOne(@PathVariable("id") Long id) {
        QueryWrapper<ShopClassProductDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        wrapper.eq( id != null,"id",id);
        ShopClassProductDetail detail = service.getOne(wrapper);
        return success(detail);
    }

    /**
     * 修改数据
     *
     * @param management 实体对象
     * @return 修改结果
     */
    @PutMapping
    public AjaxResult update(ShopClassProductDetail management) throws IOException {
        if (null != management && management.getTitle() != null) {
            service.updateById(management);
            return  success("修改成功");
        }else {
            return error("参数不能为空");
        }
    }

}
